package com.sec.android.app.myfiles.fragment.settings;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.SettingsActivity;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

/**
 * Created by seongsu.yoon on 2016-11-24.
 */

public class OpenSourceLicenseFragment extends Fragment {
    private WebView mWebView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ((SettingsActivity) getActivity()).setActionBar(R.string.about_page_open_source);
        View rootView = inflater.inflate(R.layout.open_source_license_layout, container, false);

        ((SettingsActivity) getActivity()).setScreenPath(SamsungAnalyticsLog.ScreenPath.SETTINGS_OPEN_SOURCE_LICENCES);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mWebView = (WebView) getActivity().findViewById(R.id.lisenceweb);

        try {
            if (mWebView != null) {
                mWebView.loadData(URLEncoder.encode(String.format(
                        "<html><head></head><meta name='viewport' content='width=device-width, " +
                                "user-scalable=yes'/><body><pre>%s</pre></body></html>",
                        readTextFromAsset("NOTICE")), "utf-8").replaceAll("\\+", "%20"), "text/html", "utf-8");

                WebSettings settings = mWebView.getSettings();
                settings.setUseWideViewPort(true);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
                settings.setDisplayZoomControls(false);
                settings.setLoadWithOverviewMode(true);
                settings.setDefaultFontSize(13);
            }
        } catch (UnsupportedEncodingException e) {
            Log.e(this, "UnsupportedEncodingException:" + e.toString());
        }
    }

    private String readTextFromAsset(String assetPath) {
        StringBuilder strBuilder = new StringBuilder();

        try (BufferedReader input = new BufferedReader(
                new InputStreamReader(getActivity().getApplicationContext().getAssets().open(assetPath), "UTF-8"))) {
            String str = input.readLine();
            while (str != null) {
                strBuilder.append('\n').append(str);
                str = input.readLine();
            }

        } catch (IOException e) {
            Log.e(this, "IOException:" + e.toString());
        }

        return strBuilder.toString();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
    }
}
